package com.itheima.until;

import Jama.EigenvalueDecomposition;
import Jama.Matrix;

import java.util.Arrays;

public class LinearAlgebra {

    private double a[][];
    private Matrix matrix;



    public LinearAlgebra(Matrix matrix) {
        this.matrix = matrix;
    }



    public LinearAlgebra(double[][] a) {
        Matrix matrix1 = new Matrix(a);

        this.matrix = matrix1;
    }

    public Matrix getMatrix() {
        return matrix;
    }

    public void setMatrix(Matrix matrix) {
        this.matrix = matrix;
    }

    //行列式求值
    public double determinant() {
        return matrix.det();
    }

    //解线性方程组
    public Matrix solveLinearEquations(Matrix matrixB) {
        return matrix.solve(matrixB);
    }

    //矩阵的加法
    public Matrix addMatrices(Matrix matrixA) {
        return matrix.plus(matrixA);
    }

    //矩阵的乘法
    public Matrix multiplyMatrices(Matrix matrixA) {
        return matrix.times(matrixA);
    }

    //矩阵的转置
    public Matrix transposeMatrix() {
        return matrix.transpose();
    }

    //实现逆矩阵
    public Matrix inverseMatrix() {
        return matrix.inverse();
    }

    //求矩阵的秩
    public int rankOfMatrix() {
        return matrix.rank();
    }

    //线性相关的判断
    public boolean isLinearDependent() {
        return matrix.rank() < matrix.getRowDimension();
    }

    //矩阵的特征值
    public double[] eigenvalues() {
        EigenvalueDecomposition eigenvalueDecomposition = new EigenvalueDecomposition(matrix);
        return eigenvalueDecomposition.getRealEigenvalues();
    }

//    public static boolean isSimilar(Matrix A, Matrix B) {
//        // 转换为对角矩阵
//        Matrix A_diagonal = new Matrix(A.getRowDimension(), A.getColumnDimension());
//        Matrix B_diagonal = new Matrix(B.getRowDimension(), B.getColumnDimension());
//
//        for (int i = 0; i < A.getRowDimension(); i++) {
//            A_diagonal.set(i, i, A.get(i, i));
//            B_diagonal.set(i, i, B.get(i, i));
//        }
//
//        // 计算迹
//        double traceA = A_diagonal.getTrace();
//        double traceB = B_diagonal.getTrace();
//
//        // 计算行列式
//        double detA = A_diagonal.det();
//        double detB = B_diagonal.det();
//
//        // 计算特征值
//        EigenvalueDecomposition A_eigen = new EigenvalueDecomposition(A_diagonal);
//        EigenvalueDecomposition B_eigen = new EigenvalueDecomposition(B_diagonal);
//
//        double[] eigenvaluesA = A_eigen.getRealEigenvalues();
//        double[] eigenvaluesB = B_eigen.getRealEigenvalues();
//
//        if (traceA == traceB && detA == detB && Arrays.equals(eigenvaluesA, eigenvaluesB)) {
//            return true;
//        } else {
//            return false;
//        }
//    }

    public static void main(String[] args) {
        double[][] A = {{1,3},{2,4}};
        double[][] B = {{2,4},{4,1}};
        System.out.println("B = " + B.toString());
        LinearAlgebra linearAlgebra = new LinearAlgebra(A);
        Matrix matrix = new Matrix(B);

        Matrix matrix1 = linearAlgebra.solveLinearEquations(matrix);
        double[][] array = matrix1.getArray();

        for (int i = 0; i<array.length; i++){
            for (int j = 0; j<array[i].length; j++){
                System.out.println(array[i][j]);
            }
        }
//        System.out.println("linearAlgebra.getMatrix() = " + linearAlgebra.getMatrix());
        
//        System.out.println("determinant = " + linearAlgebra.determinant());


    }

}
